using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using ENTITIES;

namespace DAL
{
  public  class TrangThaiDAL
    {
      
         private SqlConnection cn = null;
        public TrangThaiDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
       public void Insert (TBTrangThai tt)
        {
            SqlCommand cm = new SqlCommand("TrangThai_insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenTT",tt.tentt);
           
            cm.ExecuteNonQuery();
        }
        public void Update(TBTrangThai tt)
        {
            SqlCommand cm = new SqlCommand("TrangThai_update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTT", tt.matt);
            cm.Parameters.AddWithValue("@TenTT", tt.tentt);
           

            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("TrangThai_delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@MaTT", iD);
            cm.ExecuteNonQuery();
        }

        public tbTrangThaiCollection Get(string where, int pageIndex, int pageSize,
            string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("TrangThai_get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            tbTrangThaiCollection trangthaiCollection = new tbTrangThaiCollection();

            while (dr.Read())
            {
                TBTrangThai trangthai = new TBTrangThai();
                trangthai.matt = int.Parse(dr.GetValue(0).ToString());
                trangthai.tentt = dr.GetValue(1).ToString();

                trangthaiCollection.Add(trangthai);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return trangthaiCollection;
        }

    }

    }

